local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local UIManager = ClientOHOH.load.UIManager();
local PointGui=UIManager:CreateUI("PointGui",require(script.PointGui))
local Tool=require(script.Parent.Tool)
local function_list =_G.ClientOHOH.function_list
local gHp2Point=_G.CommonOHOH.config.Game.hp_Point_base
local  gRefundConst=_G.CommonOHOH.config.Game.ResetPointCost

function M.CreateData(self,dataStore)
	
	return {
		    hp=dataStore.hp,
		    damge=dataStore.damge,
		    speed=dataStore.speed,
		    lucky=dataStore.lucky,
		    points=dataStore.points,
		    costPoints=1,
		    cost={
		        base_diamond=10
		    },
		    RefundEvent=_G.CommonOHOH.load.Connect().new(),
		    UpdEvent=_G.CommonOHOH.load.Connect().new(),
		}

end


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

	self.player=self.parent
	self.DataStore=self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())

	self.Model={
		values={},--属性值
		points=0,-- 当前拥有点数
		cost=gRefundConst,--重置价格
		RefundEvent=_G.CommonOHOH.load.Connect().new(), --重置点数
		UpdEvent=_G.CommonOHOH.load.Connect().new(), --更新点数
		UsePointEvent=_G.CommonOHOH.load.Connect().new(), --使用点数
	}

	self.parent.UserDataEvent:Connect(function(data)
		if data["Points"] then
			self:UpdData(self.DataStore:GetData("Points"))
		end
	end)

	self:UpdData(self.DataStore:GetData("Points"))
	
	self.Model.RefundEvent:Connect(function()
		 return  self:refund()
	end)

	self.Model.UsePointEvent:Connect(function(id,count)
		return self:add(id,count)
  	end)

	--初始界面
	PointGui:InitData(self.Model)

	--添加点数作用监听
	self:AddValuesMonitor()

end;

-- 更新点位数据
function M.UpdData(self,data)

	self.Model.values=Tool.CalculateAttributesPoint(data)
	self.Model.points=data.had
	self.Model.UpdEvent:Action()
	UIManager.ui_list.HomeGui:SetHadPoint(self.Model.points) 

end

function M.refund(self)

	if _G.CommonOHOH.load.ItemTool().IsHadEnoughByTable(self.Model.cost,self.DataStore) then --判断消耗是否足够
		return 	self:Invoke(function_list.points_refund)
	else
		return false
	end

end


function M.add(self,id,count)
	if self.Model.points>=count then
		return 	self:Invoke(function_list.points_add,id,count)
	else --点数不够
		return false
	end
	
end

function M.AddValuesMonitor(self)
-- print("--------AddValuesMonitor--A--->")
	--训练加点
	self.player.TrainValueBaseEvent:Connect(function()
		return self.Model.values.damge
	end)

		
	--攻击加点
	self.player.AttackValueBaseEvent:Connect(function()
		return self.Model.values.damge
	end)
	
	--血量+点
	self.player.HpValueBaseEvent:Connect(function()
		return self.values.hp*gHp2Point
	end)

	--移动速度+点
	self.player.SpeedValueAddEvent:Connect(function()
		return self.Model.values.speed
	end)

	-- 幸运值+点
	self.player.LuckyValueBaseEvent:Connect(function()
		return self.Model.values.lucky
	end)

end

function M.dtor(self)
	M.super.dtor(self);
end;

return M